#include "CustomSchemeHandler.h"

bool CustomSchemeHandler::Open(CefRefPtr<CefRequest> request, bool& handle_request, CefRefPtr<CefCallback> callback)
{
	DCHECK(!CefCurrentlyOn(TID_UI) && !CefCurrentlyOn(TID_IO));
	handle_request = true;
	this->m_data = "<html><head><meta charset='utf-8'/><title>这是我自己的页面</title></head><body>这是我自己的内容</body></html>";
	return true;
}

void CustomSchemeHandler::GetResponseHeaders(CefRefPtr<CefResponse> response, int64_t& response_length, CefString& redirectUrl)
{
	CEF_REQUIRE_IO_THREAD();
	DCHECK(!m_data.empty());
	response->SetMimeType("text/html");
	response->SetStatus(200);
	response_length = m_data.length();
}

bool CustomSchemeHandler::Read(void* data_out, int bytes_to_read, int& bytes_read, CefRefPtr<CefResourceReadCallback> callback)
{
	DCHECK(!CefCurrentlyOn(TID_UI) && !CefCurrentlyOn(TID_IO));
	bool has_data = false;
	bytes_read = 0;
	if (m_offset < m_data.length())
	{
		int tansfer_size = std::min(bytes_to_read, static_cast<int>(m_data.length() - m_offset));
		memcpy(data_out, m_data.c_str() + m_offset, tansfer_size);
		m_offset += tansfer_size;
		bytes_read = tansfer_size;
		has_data = true;
	}
	return has_data;
}
